package com.gbq.qimo.servlet;

import com.gbq.qimo.pojo.Student;
import com.gbq.qimo.pojo.Teacher;
import com.gbq.qimo.service.StudentService;
import com.gbq.qimo.service.TeacherService;
import com.gbq.qimo.service.impl.StudentServiceImpl;
import com.gbq.qimo.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName updatePassword
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/5 19:39
 */
@WebServlet("/updatePassword")
public class updatePassword extends HttpServlet {
    private TeacherService teacherService = new TeacherServiceImpl();
    private StudentService studentService = new StudentServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Map<String, String[]> map = request.getParameterMap();
        if (!map.get("password2")[0].equals(map.get("password3")[0])){
            request.setAttribute("update_msg","两次密码不一样，重新输入!");
            request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
        }else{
            Object user = request.getSession().getAttribute("user");
            if (user instanceof Teacher){
                Teacher teacher = (Teacher)user;
                if (!teacher.getPassword().equals(map.get("password1")[0])){
                    request.setAttribute("update_msg","原密码输入错误!");
                    request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
                }else{
                    teacher.setPassword(map.get("password2")[0]);
                    System.out.println(teacher);
                    int count = teacherService.register(teacher);
                   if (count > 0){
                       request.setAttribute("update_msg","修改成功!");
                       request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
                   }else{
                       request.setAttribute("update_msg","修改失败!");
                       request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
                   }

                }

            }else if (user instanceof Student){
                Student student = (Student)user;
                if (!student.getPassword().equals(map.get("password1")[0])){
                    request.setAttribute("update_msg","原密码输入错误!");
                    request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
                }else{
                   student.setPassword(map.get("password2")[0]);
                    System.out.println(student);
                    int count = studentService.register(student);
                    if (count > 0){
                        request.setAttribute("update_msg","修改成功!");
                        request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
                    }else{
                        request.setAttribute("update_msg","修改失败!");
                        request.getRequestDispatcher("/updatePassword.jsp").forward(request,response);
                    }
                }
            }
        }

    }
}
